package com.example.ribbon.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.auth.domain.DataNode;
import com.example.config.auth.SecurityUtil;
import com.example.config.ribbon.ServiceCaller;
import com.example.feign.NodeFeignService;
import com.example.feign.UserFeignService;
import com.example.ribbon.service.NoteRibbonService;
import com.example.util.Results;
import com.example.util.constant.NodeUrlConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;

@Service
public class NoteRibbonServiceImpl implements NoteRibbonService {

    @Autowired
    @Lazy
    ServiceCaller serviceCaller;

    @Autowired
    private NodeFeignService nodeFeignService;

    @Autowired
    private UserFeignService userFeignService;

    @Override
    public JSONObject noteUserSave(String title, String typeId, String tag, String content) {
        String serverName=SecurityUtil.getCurrentUserServerName();
        JSONObject request = new JSONObject();
        request.put("titleName", title);
        request.put("titleTypeId", typeId);
        request.put("titleTag", tag);
        request.put("content", content);
        return serviceCaller.post(serverName, NodeUrlConstant.NOTE_SAVEORUPDATE,request, JSONObject.class);
    }

    @Override
    public JSONObject noteUserUpdate(String id, String title, String typeId, String tag, String content) {
        String serverName=SecurityUtil.getCurrentUserServerName();
        JSONObject request = new JSONObject();
        request.put("id", id);
        request.put("titleName", title);
        request.put("titleTypeId", typeId);
        request.put("titleTag", tag);
        request.put("content", content);
        return serviceCaller.post(serverName, NodeUrlConstant.NOTE_SAVEORUPDATE,request, JSONObject.class);
    }

    @Override
    public JSONObject noteGetOne(String nodeId, String noteId) {
        DataNode dataNode =nodeFeignService.getDataNodeConfigById(nodeId);
        if(dataNode ==null){
            return Results.error("数据节点获取异常");
        }
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("noteId",noteId);
        String url = UriComponentsBuilder
                .fromUriString(NodeUrlConstant.NOTE_GETONE)
                .queryParams(params)
                .toUriString();
        return serviceCaller.get(dataNode.getNodeServerName(),url,JSONObject.class,new JSONObject());
    }

    @Override
    public JSONObject noteGetOneInfo(String nodeId, String noteId) {
        DataNode dataNode =nodeFeignService.getDataNodeConfigById(nodeId);
        if(dataNode ==null){
            return Results.error("数据节点获取异常");
        }
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("noteId",noteId);
        String url = UriComponentsBuilder
                .fromUriString(NodeUrlConstant.NOTE_GETONEINFO)
                .queryParams(params)
                .toUriString();
        return serviceCaller.get(dataNode.getNodeServerName(),url,JSONObject.class,new JSONObject());
    }

    @Override
    public JSONObject notePageUser(String userId, Integer pageNum, Integer pageSize, String keyword) {
        DataNode dataNode =userFeignService.findNodeByUserId(userId);
        if(dataNode ==null){
            return Results.error("数据节点获取异常");
        }
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("userId",userId);
        params.add("pageNum",pageNum.toString());
        params.add("pageSize",pageSize.toString());
        if(keyword!=null){
            params.add("keyword",keyword);
        }
        String url = UriComponentsBuilder
                .fromUriString(NodeUrlConstant.NOTE_PAGE_USER)
                .queryParams(params)
                .toUriString();
        return serviceCaller.get(dataNode.getNodeServerName(),url,JSONObject.class,new JSONObject());
    }
}
